package com.stylefeng.guns.modular.channel.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.channel.service.IChannelService;
import com.stylefeng.guns.modular.system.model.Channel;
import com.stylefeng.guns.modular.system.model.ChannelSuperVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.ChannelSuper;
import com.stylefeng.guns.modular.channel.service.IChannelSuperService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主渠道控制器
 *
 * @author fengshuonan
 * @Date 2018-08-26 12:08:48
 */
@Controller
@RequestMapping("/channelSuper")
public class ChannelSuperController extends BaseController {

    private String PREFIX = "/channel/channelSuper/";

    @Autowired
    private IChannelSuperService channelSuperService;

    @Autowired
    private IChannelService channelService;

    /**
     * 跳转到主渠道首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "channelSuper.html";
    }

    /**
     * 跳转到添加主渠道
     */
    @RequestMapping("/channelSuper_add")
    public String channelSuperAdd() {
        return PREFIX + "channelSuper_add.html";
    }

    /**
     * 跳转到修改主渠道
     */
    @RequestMapping("/channelSuper_update/{channelSuperId}")
    public String channelSuperUpdate(@PathVariable Integer channelSuperId, Model model) {
        ChannelSuper channelSuper = channelSuperService.selectById(channelSuperId);
        model.addAttribute("item",channelSuper);
        LogObjectHolder.me().set(channelSuper);
        return PREFIX + "channelSuper_edit.html";
    }

    /**
     * 获取主渠道列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String channelName) {
        EntityWrapper<ChannelSuper> costServiceEntityWrapper = new EntityWrapper<>();
        costServiceEntityWrapper.like("channel_name", channelName);
        List<Map<String, Object>> superChannelList = channelSuperService.selectMaps(costServiceEntityWrapper);


        int totalSubCount = 0;
        int totalDownloadCount = 0;
        int totalActiveCount = 0;
        int totalRegisterCount = 0;
        int totalRequestCount = 0;
        int totalBargainCount = 0;
        BigDecimal totalBargainAmount = new BigDecimal(0.00);




        for (Map<String, Object> superChannelMap: superChannelList) {
            int downloadCount = 0;
            int activeCount = 0;
            int registerCount = 0;
            int requestCount = 0;
            int bargainCount = 0;
            BigDecimal bargainAmount = new BigDecimal(0.00);
            EntityWrapper entityWrapper = new EntityWrapper();
            entityWrapper.where("super_channel_id={0}", superChannelMap.get("id"));
            List<Map<String, Object>> channelList = channelService.selectMaps(entityWrapper);
            superChannelMap.put("subCount", channelList.size());
//            for (Map<String, Object> channelMap: channelList) {
//                downloadCount = downloadCount + (Integer) channelMap.get("downloadCount");
//                activeCount = activeCount + (Integer) channelMap.get("activeCount");
//                registerCount = registerCount + (Integer) channelMap.get("registerCount");
//                requestCount = requestCount + (Integer) channelMap.get("requestCount");
//                bargainCount = bargainCount + (Integer) channelMap.get("bargainCount");
//                bargainAmount = bargainAmount.add((BigDecimal) channelMap.get("bargainAmount"));
//            }
//            superChannelMap.put("downloadCount", downloadCount);
//            superChannelMap.put("activeCount", activeCount);
//            superChannelMap.put("registerCount", registerCount);
//            superChannelMap.put("requestCount", requestCount);
//            superChannelMap.put("bargainCount", bargainCount);
//            superChannelMap.put("bargainAmount", bargainAmount);
            superChannelMap.put("status", StatusEnum.desc((Integer) superChannelMap.get("isEnabled")));
//
//
//            totalSubCount = totalSubCount + channelList.size();
//            totalDownloadCount = totalDownloadCount + downloadCount;
//            totalActiveCount = totalActiveCount + activeCount;
//            totalRegisterCount = totalRegisterCount + registerCount;
//            totalRequestCount = totalRequestCount + requestCount;
//            totalBargainCount = totalBargainCount + bargainCount;
//            totalBargainAmount = totalBargainAmount.add(bargainAmount);

        }
//        HashMap<String, Object> countMap = new HashMap<>();
//        countMap.put("channelName", superChannelList.size());
//        countMap.put("subCount", totalSubCount);
//        countMap.put("downloadCount", totalDownloadCount);
//        countMap.put("activeCount", totalActiveCount);
//        countMap.put("registerCount", totalRegisterCount);
//        countMap.put("requestCount", totalRequestCount);
//        countMap.put("bargainCount", totalBargainCount);
//        countMap.put("bargainAmount", totalBargainAmount);
//        superChannelList.add(0, countMap);
        return superChannelList;
    }

    /**
     * 新增主渠道
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ChannelSuperVo channelSuperVo) {
        String channelSubName = channelSuperVo.getChannelSubName();
        ChannelSuper channelSuper = channelSuperVo;
        channelSuper.setCreateTime(new Date());
        channelSuperService.insert(channelSuper);

        Channel channel = new Channel();
        channel.setSuperChannelId(channelSuper.getId());
        channel.setChannelName(channelSubName);
        channel.setIsEnabled(1);
        channel.setCreateTime(new Date());

        channelService.insert(channel);
        return SUCCESS_TIP;
    }

    /**
     * 删除主渠道
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer channelSuperId) {
        channelSuperService.deleteById(channelSuperId);
        return SUCCESS_TIP;
    }

    /**
     * 修改主渠道
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ChannelSuper channelSuper) {
        channelSuperService.updateById(channelSuper);
        return SUCCESS_TIP;
    }

    /**
     * 主渠道详情
     */
    @RequestMapping(value = "/detail/{channelSuperId}")
    @ResponseBody
    public Object detail(@PathVariable("channelSuperId") Integer channelSuperId) {
        return channelSuperService.selectById(channelSuperId);
    }

    /**
     * 查询主渠道列表
     * @param
     * @return
     */
    @RequestMapping(value = "/queryChannelList")
    @ResponseBody
    public Object queryChannelList() {
        List<ChannelSuper> channelSupers = channelSuperService.selectList(null);
        return channelSupers;
    }
}
